﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace StructGoTest.GameObjects {

	public class Developer : GameObject {

		private double DirectionDuration = 1f; // Seconds
		private double DirectionDurantionElapsed;

		// todo remove
		//bool RotatingDown;


		public Developer(float xLocation) {

			// Body
			HasBody = true;
			Height = BlobTestGame.BusinessGuyBody.Height;
			Width = BlobTestGame.BusinessGuyBody.Width;
			Position = new Vector2(xLocation, BlobTestGame.Floor - Height);  // Shared with: Move, Jump

			// Think
			HasMind = true;

			// Move
			CanMove = true;
			BaseHorizontalMoveSpeed = 50;
			CanJump = true;
			HorizontalJumpSpeed = 100;
			VerticalJumpSpeed = -100;
			Direction = Direction.Left;

			// Jump
			HorizontalJumpSpeed = 100;

			// Draw
			IsDrawable = true;
			Texture = BlobTestGame.BusinessGuyBody;
			Color = Color.White;

			// Head
			HasHead = true;
			HeadTexture = BlobTestGame.WhiteHead;
			Neck = new Vector2(Position.X+10, Position.Y);
			TopOfHead = new Vector2(Neck.X, Neck.Y - HeadTexture.Height);
			RotationSpeed = 60; // Degrees per second.
			HeadGear = BlobTestGame.CowboyHat;
			HeadBounds = new Rectangle((int)Position.X, (int)Position.Y, HeadTexture.Width, HeadTexture.Height);
			CenterOfHead = new Vector2(HeadBounds.Center.X, HeadBounds.Center.Y);
		}


		public override void Think(GameTime gameTime) {
			
			DirectionDurantionElapsed += gameTime.ElapsedGameTime.TotalSeconds;
			if (OnSolidGround) {
				if (DirectionDurantionElapsed > DirectionDuration) {

					int decision = Random.Next(100);

					if (decision < 25) {
						Direction = Direction.Left;
						CurrentHorizontalSpeed = BaseHorizontalMoveSpeed;
					} else if (decision < 75) {
						CurrentHorizontalSpeed = 0;
					} else {
						Direction = Direction.Right;
						CurrentHorizontalSpeed = BaseHorizontalMoveSpeed;
					}

					DirectionDurantionElapsed -= DirectionDuration;
				}
			}

			// TODO Jump
			if (BlobTestGame.PreviousKeyboardState.IsKeyUp(Keys.J) && BlobTestGame.CurrentKeyboardState.IsKeyDown(Keys.J)) {
				CurrentVerticalSpeed += VerticalJumpSpeed;
				OnSolidGround = false;
			}
			// TODO Jump

			//TODO Look at

			if (Direction == Direction.Left) {
				if (BlobTestGame.LookAtPoint.X < HeadBounds.Left) {
					HeadAngle = BlobTestGame.GetAngleOfLineBetweenTwoPoints(HeadBounds.Center, BlobTestGame.LookAtPoint, true) + 180;
				} else {
					HeadAngle = 0;
				}
			} else if (Direction == Direction.Right) {
				if (BlobTestGame.LookAtPoint.X > HeadBounds.Right) {
					HeadAngle = BlobTestGame.GetAngleOfLineBetweenTwoPoints(HeadBounds.Center, BlobTestGame.LookAtPoint, true);
				} else {
					HeadAngle = 0;
				}
			}
		}


		public override void Move(GameTime gameTime) {

			float xDelta = CurrentHorizontalSpeed * (float)Direction * (float)gameTime.ElapsedGameTime.TotalSeconds;
			float yDelta = CurrentVerticalSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

			Position = new Vector2(Position.X + xDelta, Position.Y + yDelta);

			if (Position.X < 0) {
				Position.X = 0;
			} else if (Position.X > 1280 - Width) {
				Position.X = 1280 - Width;
			}

			SetHeadLocation();
		}


		public override void Draw(GameTime gametime) {

			// Draw head.
			BlobTestGame.SpriteBatch.Draw(HeadTexture,
										  new Rectangle((int)(Neck.X),
														(int)(Neck.Y - HeadTexture.Height / 2f + 2),
														HeadTexture.Width,
														HeadTexture.Height),
										  null,
										  Color.White,
										  MathHelper.ToRadians(HeadAngle), // Rotation
										  new Vector2(HeadTexture.Width / 2f, HeadTexture.Height / 2f), // Origin (center)
										  Direction == Direction.Right ? SpriteEffects.None : SpriteEffects.FlipHorizontally,
										  0); // LayerDepth

			base.Draw(gametime);

			//=====( Begin Debug Drawing )======

			// Draw neck.
			BlobTestGame.SpriteBatch.Draw(BlobTestGame.Blank, new Rectangle((int)Neck.X - 5, (int)Neck.Y - 5, 10, 10), Color.DarkRed);

			// Draw top of head.
			BlobTestGame.SpriteBatch.Draw(BlobTestGame.Blank, new Rectangle((int)TopOfHead.X - 5, (int)TopOfHead.Y - 5, 10, 10), Color.DarkRed);

			//=====( End Debug Drawing )=======

			// Draw Head Gear
			BlobTestGame.SpriteBatch.Draw(HeadGear.Texture,
										  new Rectangle((int)(TopOfHead.X - HeadGear.Offset.X),
														(int)(TopOfHead.Y - HeadGear.Offset.Y),
														HeadGear.Texture.Width,
														HeadGear.Texture.Height),
										  null,
										  Color.White,
										  MathHelper.ToRadians(HeadAngle), // Rotation
										  HeadGear.Origin, // Origin (center)
										  Direction == Direction.Right ? SpriteEffects.None : SpriteEffects.FlipHorizontally,
										  0); // LayerDepth
		}

	}

}